package jdir.dialog;

import java.io.File;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import jdir.dialog.exceptions.OutOfRowCountException;
import jdir.util.MessagesUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class holds the contents of the actual directory 
 * @author Marco Peroverde
 *
 */
@SuppressWarnings("unused")
public class FileDataModel extends AbstractTableModel
{

	private static final long	serialVersionUID	= -6050673176444932094L;
	private static Log			LOG					= LogFactory.getLog(FileDataModel.class);
	private static Boolean		DEBUG				= LOG.isDebugEnabled();
	private static Boolean		TRACE				= LOG.isTraceEnabled();

	private ArrayList<File>		fileList			= new ArrayList<File>();

	/**
	 * This constructor takes an array of File objects wich it will
	 * store for showing it in the {@link JDirMainDlg}
	 * @param fileList array of file objects
	 */
	public FileDataModel(File[] fileList)
	{
		for (int i = 0; i < fileList.length; i++)
			this.fileList.add(fileList[i]);
	}

	public Class<?> getColumnClass(int col)
	{
		if (TRACE)
			LOG.trace("FileDataModel.getColumnClass called with: " + col);
		switch (col)
		{
		case 0:
			return String.class;
		case 1:
			return String.class;
		case 2:
			return Integer.class;
		case 3:
			return String.class;
		default:
			return null;
		}
	}

	public int getColumnCount()
	{
		return 4;
	}

	/**
	 * Gets the right columntitle from the messages.properties file and returns it.
	 * @param col columnnumber for witch the columnName is needed.
	 * @return coloumnname in the default language
	 */
	public String getColumnName(int col)
	{
		if (TRACE)
			LOG.trace("FileDataModel.getColumnName called with: " + col);
		switch (col)
		{
		case 0:
			return MessagesUtil.getMessage("jdir.dialog.FileDataModel.columnTitle.name");
		case 1:
			return MessagesUtil.getMessage("jdir.dialog.FileDataModel.columnTitle.type");
		case 2:
			return MessagesUtil.getMessage("jdir.dialog.FileDataModel.columnTitle.size");
		case 3:
			return MessagesUtil.getMessage("jdir.dialog.FileDataModel.columnTitle.rigths");
		default:
			return null;
		}
	}

	/**
	 * Gets the count of the currently listed files and returns it
	 * @return Count of currently listet files which may also be directories 
	 */
	public int getRowCount()
	{
		return fileList.size();
	}

	/**
	 * This method returns the file which is currently listet in row x
	 * @param row which file is needed
	 * @return the file listed in the given row. Null if there is no file in that row
	 * @throws OutOfRowCountException will be thrown if the given row count is invalid
	 */
	public File getFileAt(int row) throws OutOfRowCountException
	{
		if (fileList.size() < row || fileList.size() == 0)
			throw new OutOfRowCountException();
		return fileList.get(row);
	}

	/**
	 * Returns the value for the given cell which either represents the name of a file
	 * or the type or the length or the rights which are set for this file object.
	 * @param row desired row
	 * @param col desired column
	 * @return Depends on the column 0, 1, 3, will be strings 2 a long object
	 */
	public Object getValueAt(int row, int col)
	{
		File file = fileList.get(row);

		switch (col)
		{
		case 0:
			return new String(file.getName());
		case 1:
			return new String(file.isFile() ? "File" : "Dir");
		case 2:
			return new Long(file.length());
		case 3:
		{
			//Test for reading, writing, excecuting and write the result to the string
			//r-0-x would then mean that we can read that file but cant write to it but could
			//execute it, if we wish to do so
			String rights = "";
			if (file.canRead())
				rights = "r-";
			else
				rights = "0-";
			if (file.canWrite())
				rights += "w-";
			else
				rights += "0-";
			if (file.canExecute())
				rights += "x";
			else
				rights += "0";

			return rights;
		}

		default:
			return null;
		}
	}

}
